package cn.exrick.xboot.modules.Equipments.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.Equipments.entity.Equipments;
import cn.exrick.xboot.modules.Equipments.service.IEquipmentsService;
import cn.exrick.xboot.modules.Information.entity.River;
import cn.exrick.xboot.modules.Information.service.IriverService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

/**
 * @author ZhangWei
 */
@Slf4j
@RestController
@Api(description = "设备信息管理接口")
@RequestMapping("/xboot/equipments")
@Transactional
public class EquipmentsController {

    @Autowired
    private IEquipmentsService iEquipmentsService;

    @Autowired
    private IriverService iriverService;

    @PersistenceContext
    private EntityManager entityManager;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<Equipments> get(@PathVariable Long id){

        Equipments equipments = iEquipmentsService.getById(id);
        return new ResultUtil<Equipments>().setData(equipments);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<Equipments>> getAll(){

        List<Equipments> list = iEquipmentsService.list();
        List<River> riverList = iriverService.list();

        if(list.size()>0){
            for(Equipments equipments1:list){
                for(River river:riverList){
                    if(equipments1.getRiverId().equals(river.getId())){
                        equipments1.setRiverName(river.getRiverName());
                    }
                }
            }
        }
        return new ResultUtil<List<Equipments>>().setData(list);
    }

    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<IPage<Equipments>> getByCondition(@ModelAttribute Equipments equipments,@ModelAttribute PageVo page){

        QueryWrapper<Equipments> qw = new QueryWrapper<Equipments>();
        if(equipments.getEquipmentName() != null && !"".equals(equipments.getEquipmentName())){
            qw.like("equipment_name",equipments.getEquipmentName());
        }
        if(equipments.getEquipmentType() != null && !"".equals(equipments.getEquipmentType())){
            qw.eq("equipment_type",equipments.getEquipmentType());
        }
        if(equipments.getRiverId() != null && !"".equals(equipments.getRiverId())){
            qw.eq("river_id",equipments.getRiverId());
        }

        List<River> list = iriverService.list();

        IPage<Equipments> data = iEquipmentsService.page(PageUtil.initMpPage(page),qw);

        if(data.getRecords().size()>0){
            for(Equipments equipments1:data.getRecords()){
                for(River river:list){
                    if(equipments1.getRiverId().equals(river.getId())){
                        equipments1.setRiverName(river.getRiverName());
                    }
                }
            }
        }
        return new ResultUtil<IPage<Equipments>>().setData(data);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<Equipments>> getByPage(@ModelAttribute PageVo page){

        IPage<Equipments> data = iEquipmentsService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<Equipments>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<Equipments> saveOrUpdate(@ModelAttribute Equipments equipments){

        if(iEquipmentsService.saveOrUpdate(equipments)){
            return new ResultUtil<Equipments>().setData(equipments);
        }
        return new ResultUtil<Equipments>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable Long[] ids){

        for(Long id : ids){
            iEquipmentsService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }
}
